const http = require("http");
const fs = require("fs");
const path = require("path");

const server = http.createServer();

server.on("request", (request, response) => {
  if (request.url === "/index.html") {
    fs.readFile(path.join(__dirname, "dist/index.html"), (err, data) => {
      if (err) throw err;
      else {
        const html = data.toString();
        response.setHeader("Content-Type", "text/html;charset=utf-8");
        response.end(html);
      }
    });
  } else {
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    response.end("您访问的路径不存在");
  }
});

server.listen(9001, () => {
  console.log("服务器启动成功,浏览器访问http://localhost:9001");
});
